<template>
  <div class="nav-menu">
    <div class="logo">
      <img class="img" src="@/assets/logo-g.png" alt="logo" />
      <span v-if="!isCollapse" class="title">xiaoming</span>
    </div>
    <el-menu
      :collapse="isCollapse"
      :default-active="nowActiveRouteID"
      class="el-menu-vertical"
      background-color="#0c2135"
      text-color="#b7bdc3"
      active-text-color="#0a60bd"
    >
      <template v-for="menu in menus" :key="menu.id">
        <!-- 二级菜单 -->
        <template v-if="menu.type === 1">
          <!-- 二级菜单 可以展开的标题 -->
          <el-sub-menu :index="menu.id + ''">
            <template #title>
              <!-- <i v-if="menu.icon" :class="menu.icon"></i> -->
              <!-- <el-icon><ex-slot :tag="menu.icon" /></el-icon> -->
              <!-- <el-icon>{{ menu.icon }}</el-icon> -->
              <!-- <div>{{ menu.icon }}</div> -->
              <!-- 原文解决方案 https://blog.csdn.net/Royzilong/article/details/124152005 -->
              <el-icon :size="20"
                ><component :is="menu.icon.slice(8)"></component
              ></el-icon>
              <span>{{ menu.name }}</span>
            </template>
            <!-- 二级菜单的每个item -->
            <template v-for="item in menu.children" :key="item.id">
              <el-menu-item
                :index="item.id + ''"
                @click="handleRouteChange(item)"
              >
                <i v-if="item.icon" :class="item.icon"></i>
                <span>{{ item.name }}</span>
              </el-menu-item>
            </template>
          </el-sub-menu>
        </template>
        <!-- 一级菜单 -->
        <template v-else-if="menu.type === 2">
          <el-menu-item :index="menu.id + ''">
            <i v-if="menu.icon" :class="menu.icon"></i>
            <span>{{ menu.name }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script lang="ts">
/* const exSlot = {
  functional: true,
  props: {
    tag: String,
  },
  render: (h: any, context: any) => {
    return h(context.props.tag, null, null);
  },
}; */

import { defineComponent, computed, ref } from 'vue';

import { useVuex } from '@/store/index';

import { useRouter, useRoute } from 'vue-router';

import { findMenuToPath } from '@/utils/map-route';

/**
 * el-sub-menu 折叠menu
 * el-menu-item 可点击的单个menu
 * el-menu-item-group 菜单组
 *
 * 每个菜单的type
 * 当是1的时候是可展开
 * 当是2的时候可点击的菜单
 * 当是3的时候是控制详细菜单
 */
export default defineComponent({
  components: {
    // exSlot,
  },
  props: {
    isCollapse: {
      type: Boolean,
      default: false,
    },
  },
  setup() {
    const store = useVuex();
    const router = useRouter();
    const menus = computed(() => store.state.LoginModule.menus); // 返回的ref类型的数据格式

    const handleRouteChange = (item: any): void => {
      router.push({
        path: item.url ?? '/404',
      });
    };

    const route = useRoute();
    const menu = findMenuToPath(menus.value, route.path);
    // console.log(menu);
    const nowActiveRouteID = ref(menu?.id + '');

    return {
      menus,
      handleRouteChange,
      nowActiveRouteID,
    };
  },
});
</script>

<style scoped lang="less">
.nav-menu {
  height: 100%;
  background-color: #001529;

  .logo {
    display: flex;
    height: 28px;
    padding: 12px 10px 8px 10px;
    flex-direction: row;
    justify-content: flex-start;
    align-items: center;

    .img {
      height: 100%;
      margin: 0 10px;
    }

    .title {
      font-size: 16px;
      font-weight: 700;
      color: white;
    }
  }

  .el-menu {
    border-right: none;
  }

  // 目录
  .el-submenu {
    background-color: #001529 !important;
    // 二级菜单 ( 默认背景 )
    .el-menu-item {
      padding-left: 50px !important;
      background-color: #0c2135 !important;
    }
  }

  ::v-deep .el-submenu__title {
    background-color: #001529 !important;
  }

  // hover 高亮
  .el-menu-item:hover {
    color: #fff !important; // 菜单
  }

  .el-menu-item.is-active {
    color: #fff !important;
    background-color: #0a60bd !important;
  }
}

.el-menu-vertical:not(.el-menu--collapse) {
  width: 100%;
  height: calc(100% - 48px);
}
</style>
